package com.justgame.project.service.just.service;

import com.justgame.project.cloud.common.exception.Exc;
import com.justgame.project.cloud.common.util.ShiroUtils;
import com.justgame.project.cloud.common.util.IdUtils;
import com.justgame.project.service.api.dto.JustGameScoreDTO;
import com.justgame.project.service.just.mapper.JustGameScoreMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;


@Service
@Transactional
@Slf4j
public class JustGameScoreService {
    @Resource
    private JustGameScoreMapper scoreMapper;

    public Boolean pushScore(JustGameScoreDTO dto) {
        String userId = ShiroUtils.getUserId();
        if (StringUtils.isBlank(userId)) {
            throw new Exc("请先登陆后再试！");
        }

        dto.setSysUserId(userId);
        if (StringUtils.isBlank(dto.getGameId())) {
            throw new Exc("未知的游戏ID！");
        } else if (scoreMapper.hasScored(dto)) {
            throw new Exc("您已经对此游戏打分了哦！");
        }

        if (dto.getScore() == null || dto.getScore() <= 0 || dto.getScore() > 5) {
            throw new Exc("评分数不符合规则哦，请在1~5分之间选择吧！");
        }

        dto.setId(IdUtils.create());
        dto.setTime(LocalDateTime.now());
        return scoreMapper.add(dto) > 0;
    }

    public Map<String, Object> getScore(String id) {
        return scoreMapper.getScore(id);
    }
}
